package com.example.datasource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.sql.Connection;

public class C3P0Test {

    // 方式一：硬编码数据库配置信息
    @Test
    public void testGetConnection() throws Exception {
        // 创建c3p0连接池
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        // 设置属性
        comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://60.60.60.223:30335/test?rewriteBatchedStatements=true");
        comboPooledDataSource.setUser("root");
        comboPooledDataSource.setPassword("admin1#3");
        // 设置初始时池中连接数
        comboPooledDataSource.setInitialPoolSize(10);

        // 获取连接
        Connection connection = comboPooledDataSource.getConnection();
        System.out.println(connection);
    }

    // 方式二：使用配置文件
    @Test
    public void testGetConnection1() throws Exception {
        // 创建c3p0连接池
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("hello-c3p0");

        // 获取连接
        Connection connection = comboPooledDataSource.getConnection();
        System.out.println(connection);
    }

}
